﻿using DynamicViewFormApp.Component.Handler;
using DynamicViewFormApp.Entity;
using DynamicViewFormApp.Service;
using DynamicViewFormApp.Setting;
using DynamicViewFormApp.Util;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DynamicViewFormApp.Component
{
    /// <summary>
    /// 富文本服务类
    /// </summary>
    class RichTextBoxService
    {
        /// <summary>
        /// 创建答题卡区域
        /// </summary>
        /// <param name="page">分页对象</param>
        /// <param name="top"></param>
        /// <param name="left"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        internal static void CreateFullFillingRecognition(Control page, int top, int left, int width, int height)
        {
            RichTextBox rtb = PageService.CreateRichTextBox(page);
            rtb.ReadOnly = true;
            rtb.Location = new Point(left + width - (width / 2) - 60, top + height + 20);
            rtb.Width = width / 2 + 35;
            rtb.Height = 280;

            Font font = new Font("宋体", 9, FontStyle.Bold);
            rtb.Font = font;

            rtb.Rtf = RtfContent.FullFillingRecognitionStr;
            //rtb.MouseHover -= new EventHandler(RichTextBoxHandler.MouseHover);
            //rtb.MouseLeave -= new EventHandler(RichTextBoxHandler.MouseLeave);
        }
        /// <summary>
        /// 生成考生基础信息
        /// </summary>
        /// <param name="page"></param>
        /// <param name="top"></param>
        /// <param name="left"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        internal static void CreateTestExamineeInfo(Control page, int top, int left, int width, int height)
        {
            RichTextBox rtb = PageService.CreateRichTextBox(page);
            rtb.ReadOnly = true;
            rtb.Location = new Point(left + 40, top + height + 20);
            rtb.Width = width / 2 - 120;
            rtb.Height = 280;

            Font font = new Font("宋体", 9, FontStyle.Bold);
            rtb.Font = font;

            rtb.Text = RtfContent.ExamineeInfoStr;
            //rtb.MouseHover -= new EventHandler(RichTextBoxHandler.MouseHover);
            //rtb.MouseLeave -= new EventHandler(RichTextBoxHandler.MouseLeave);
        }

        /// <summary>
        /// 生成考试标题
        /// </summary>
        /// <param name="page"></param>
        internal static RichTextBox CreateTestTitleElement(Control page)
        {
            RichTextBox rtb = PageService.CreateRichTextBox(page);
            Font font = FontSetting.FontHeader4;
            rtb.Font = font;
            rtb.Text = ExamInfo.ExamName;
            rtb.SelectionAlignment = HorizontalAlignment.Center;  //RichTextBox中的文本居中对齐
            rtb.TextChanged += new EventHandler(ExamInfoHandler.ExamName_Changed);
            return rtb;
        }

        /// <summary>
        /// 生成题块区域
        /// </summary>
        /// <param name="page"></param>
        internal static List<RichTextBox> CreateBlockContentArea(Control page)
        {
            int pageOffSet = PageSetting.PageOffSet;
            List<PageEntity> positionList = PageSetting.PageLocationList;
            List<RichTextBox> richTextBoxes = new List<RichTextBox>();
            foreach (PageEntity p in positionList)
            {
                int top = p.Top;
                int left = p.Left;
                int width = p.Width;
                int height = p.Height;

                int maxX = left;
                int maxY = top;
                foreach (Control ctl in page.Controls)
                {
                    int cX = ctl.Location.X;
                    int cY = ctl.Location.Y;
                    int cHeight = ctl.Height;
                    int cWidth = ctl.Width;
                    bool result = DrawUtil.IsPointInRect(top, left, width, height, cX, cY);
                    if (result)
                    {
                        maxX = cX < maxX ? cX : maxX;
                        maxY = (cY + cHeight) > maxY ? cY + cHeight : maxY;
                    }
                }


                RichTextBox rtb = PageService.CreateRichTextBox(page);

                rtb.Location = new Point(maxX + pageOffSet / 2, maxY + pageOffSet / 2 + 2);

                rtb.Width = width - 2 * pageOffSet;
                rtb.Height = height - maxY - pageOffSet / 2 - 1;

                //rtb.BorderStyle = BorderStyle.FixedSingle;

                //rtb.MouseHover -= new EventHandler(RichTextBoxHandler.MouseHover);
                //rtb.MouseLeave -= new EventHandler(RichTextBoxHandler.MouseLeave);
                //rtb.GotFocus += new EventHandler(RichTextBoxHandler.GotFocus);

                rtb.MouseClick += new MouseEventHandler(RichTextBoxHandler.BlockContent_Click);
                rtb.TextChanged += new System.EventHandler(RichTextBoxHandler.RichTextBox_TextChanged);

                rtb.ContentsResized += new ContentsResizedEventHandler(RichTextBoxHandler.ContentsResized);
                rtb.ScrollBars = RichTextBoxScrollBars.None;

                richTextBoxes.Add(rtb);

                PageSetting.RichTextBoxeList.Add(rtb);
            }

            Image image = new Bitmap(page.BackgroundImage);
            Graphics graphics = Graphics.FromImage(image);
            Pen pen = new Pen(Color.Black, 2f);
            int pageOffSet2 = PageSetting.PageOffSet;
            foreach (RichTextBox rtb in richTextBoxes)
            {
                int cX = rtb.Location.X;
                int cY = rtb.Location.Y;
                int cHeight = rtb.Height;
                int cWidth = rtb.Width;

                graphics.DrawRectangle(pen, cX - pageOffSet2 / 2, cY - pageOffSet2 / 2, cWidth + pageOffSet2, cHeight + pageOffSet2);
            }

            //将位图贴到窗口上
            page.BackgroundImage = image;
            //释放gdi和pen资源
            graphics.Dispose();
            pen.Dispose();

            return richTextBoxes;
        }
    }
}
